﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Text.RegularExpressions;

namespace PSE.Deployment.Util
{
    public class Oracle
    {
        public static bool CheckInstalled()
        {
            return WindowsRegistry.KeyExists(@"SOFTWARE\ORACLE\ODP.NET\2.112.1.0",
                                   Registry.LocalMachine);
        }

        public static List<string> GetOracleHomes()
        {
            List<string> oracleHomes = new List<string>();

            try
            {
                RegistryKey rgkLM = Registry.LocalMachine;
                RegistryKey rgkAllHome = rgkLM.OpenSubKey(@"SOFTWARE\ORACLE");
                if (rgkAllHome != null)
                {
                    foreach (string subkey in rgkAllHome.GetSubKeyNames())
                    {
                        if (subkey.StartsWith("KEY_"))
                            oracleHomes.Add(subkey);
                    }
                }
            }
            catch
            {
                //TODO: Logar Erro
            }

            return oracleHomes;
        }

        private static string GetOracleHomePath(String OracleHomeRegistryKey)
        {
            RegistryKey rgkLM = Registry.LocalMachine;
            RegistryKey rgkOracleHome = rgkLM.OpenSubKey(@"SOFTWARE\ORACLE\" +
                OracleHomeRegistryKey);

            if (!rgkOracleHome.Equals(""))
                return rgkOracleHome.GetValue("ORACLE_HOME").ToString();
            return "";
        }

        private static string GetTNSNAMESORAFilePath(String OracleHomeRegistryKey)
        {
            string oracleHomePath = GetOracleHomePath(OracleHomeRegistryKey);
            string tnsNamesOraFilePath = "";
            if (!oracleHomePath.Equals(""))
            {
                tnsNamesOraFilePath = oracleHomePath + @"\NETWORK\ADMIN\TNSNAMES.ORA";
                if (!(System.IO.File.Exists(tnsNamesOraFilePath)))
                {
                    tnsNamesOraFilePath = oracleHomePath + @"\NET80\ADMIN\TNSNAMES.ORA";
                }
            }
            return tnsNamesOraFilePath;
        }

        public static List<string> LoadTNSNames(string OracleHomeRegistryKey)
        {
            List<string> TNSNamesCollection = new List<string>();

            try
            {
                string RegExEnter = @"[\n]";
                string RegExPattern = @"[\s]*[^\(][a-zA-Z0-9_\-.]+[\s]*=[\s]*\(";
                string strTNSNAMESORAFilePath = GetTNSNAMESORAFilePath(OracleHomeRegistryKey);

				TNSNamesCollection = FillTNSList(RegExPattern, strTNSNAMESORAFilePath, true);
                TNSNamesCollection.AddRange(FillTNSList(string.Concat(RegExEnter, RegExPattern), strTNSNAMESORAFilePath, false));
            }
            catch
            {
                //TODO: Logar Erro
            }

            return TNSNamesCollection;
        }

        private static List<string> FillTNSList(string RegExPattern, string strTNSNAMESORAFilePath, bool isUnique)
        {
            List<string> DBNamesCollection = new List<string>();

            try
            {
                if (!strTNSNAMESORAFilePath.Equals(""))
                {
                    System.IO.FileInfo fiTNS = new System.IO.FileInfo(strTNSNAMESORAFilePath);
                    if (fiTNS.Exists)
                    {
                        if (fiTNS.Length > 0)
                        {
                            //lendo tnsnames.ora
                            int iCount = 0;

                            if (isUnique)
                            {
                                DBNamesCollection.Add(Regex.Matches(
                                        System.IO.File.ReadAllText(fiTNS.FullName),
                                        RegExPattern)[iCount].Value.Trim().Substring(0,
                                        Regex.Matches(System.IO.File.ReadAllText(fiTNS.FullName),
                                        RegExPattern)[iCount].Value.Trim().IndexOf(" ")).ToUpper());
                            }
                            else
                            {
                                for (iCount = 0; iCount < Regex.Matches(
                                    System.IO.File.ReadAllText(fiTNS.FullName),
                                    RegExPattern).Count; iCount++)
                                {
                                    DBNamesCollection.Add(Regex.Matches(
                                        System.IO.File.ReadAllText(fiTNS.FullName),
                                        RegExPattern)[iCount].Value.Trim().Substring(0,
                                        Regex.Matches(System.IO.File.ReadAllText(fiTNS.FullName),
                                        RegExPattern)[iCount].Value.Trim().IndexOf(" ")).ToUpper());
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                //TODO: Logar Erro
            }

            return DBNamesCollection;
        }
    }
}
